<?php include "checkSessionForAuthen.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Thông Tin Tuyển Dụng</title>
        <?php include("import.php"); ?>
        <script type="text/javascript">
            function submitForm(){
                var form = document.getElementById("form");
                form.method = "post";
                form.action = "../action/deleteRecruitmentAction.php";
                form.submit();
            }
			
            function confirmDelete(id) {
                if (confirm('Bạn có chắc muốn xóa không?')) {
                    window.location = "../action/deleteRecruitmentAction.php?recId=" + id;
                }
            }
        
        </script>
    </head>

    <body>
        <!--header-->
        <?php
        include("../service/recruitmentService.php");
        include "header.php";
        ?>
        <!--end header-->
        <!-- Container -->
        <?php
        /* process paging event */
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 1;
        }

        $row_per_page = 10;


        if (isset($keyword)) {
            $keyword = $_GET['keyword'];
        } else {
            $keyword = "";
        }

        $total = countNumOfRecruitmentByName($keyword);
        $total_page = ceil($total / $row_per_page);
        $start_position = ($page_num * $row_per_page) - ($row_per_page);
        ?>
        <form id="form" action="recruitment.php" method="post" name="form">
            <!-- Container -->
            <div id="container">
                <div class="shell">
                    <!-- Message OK -->
                    <?php
                    $message = "";
                    if (isset($_GET['message'])) {
                        $deleteMessage = $_GET['message'];
                    }
                    if (isset($deleteMessage)) {
                        if ($deleteMessage == 'ok') {
                            $message = 'Thông tin đã được xóa thành công!';
                        } else if ($deleteMessage == 'editOk') {
                            $message = 'Thông tin đã được chỉnh sửa thành công!';
                        } else {
                            $message = 'Không thể xóa thông tin!';
                        }
                    }

                    if ($message == '') {
                        $class = 'msg msg-ok msg-hidden';
                    } else {
                        $class = 'msg msg-ok';
                    }
                    ?>	
                    <div class="<?php echo($class); ?>" id="msgOK">
                        <p><strong><?php echo($message); ?></strong></p>
                        <a href="#" onclick="hideMessage()" class="close">close</a>
                    </div>
                    <!-- End Message OK -->

                    <!-- Message Error -->
                    <div class="msg msg-error msg-hidden">
                        <p><strong>You must select a file to upload first!</strong></p>
                        <a href="#" class="close">close</a>
                    </div>
                    <!-- End Message Error -->

                    <br />
                    <!-- Main -->
                    <div id="main">
                        <div class="cl">&nbsp;</div>

                        <!-- Content -->
                        <div id="content">

                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2 class="left">Danh Sách Thông Tin Tuyển Dụng</h2> 
                                    <div class="right">
                                        <label>Tìm Kiếm:</label>
                                        <input type="text" class="field small-field" name="keyword" />
                                    </div>
                                </div>
                                <!-- End Box Head -->	
                                <!-- Table -->
                                <div class="table" id="tblRecruitment">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <th width="13"></th>
                                            <th>STT</th>
                                            <th>Thông Tin Tuyển Dụng</th>
                                            <th width="110" class="ac">Thao Tác</th>
                                        </tr>
                                        <?php
                                        $count = 1;
                                        $result = getRecruitmentBy($keyword, $start_position, $row_per_page);

                                        while ($row = ConnectionManager::getInstance()->fetchArray()) {
                                            ?>
                                            <tr>
                                                <td>
                                                    <input type="checkbox" class="checkbox" value="<?php echo $row["REC_ID"]; ?>" name="checkbox[]" id="checkbox[]"/>
                                                </td>
                                                <td><?php echo $count; ?></td>
                                                <td>
                                                    <h3>
                                                        <a href="recruitmentDetail.php?id=<?php echo $row["REC_ID"]; ?>">
                                                            <?php
                                                            $str = preg_replace("/<.*?>/", "", trim($row["REC_CONTENT"]));
                                                            echo substr($str, 1, 100) . "...";
                                                            ?>
                                                        </a>
                                                    </h3>
                                                </td>
                                                <td>
                                                    <a href="#" class="ico del" onclick="confirmDelete(<?php echo $row["REC_ID"]; ?>);">Delete</a>
                                                    <a href="recruitmentDetail.php?id=<?php echo $row["REC_ID"]; ?>" class="ico edit">Edit</a></td>
                                            </tr>
                                            <?php
                                            $count++;
                                        }
                                        ?>
                                    </table>
                                    <!-- Pagging -->
                                    <div class="pagging">
                                        <div class="left">Trang <?php echo(" " . $page_num . "  trong tổng số " . $total_page . " trang "); ?></div>
                                        <div class="right">
                                            <?php
                                            for ($i = 1; $i <= $total_page; $i++) {
                                                echo("<a href='" . "recruitment.php?" . "&keyword=" . $keyword . "&page_num=" . $i . "'>" . $i . "</a>");
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <!-- End Pagging -->
                                </div>
                                <!-- Table -->
                            </div>
                            <!-- End Box -->

                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2>Thao tác</h2>
                                </div>
                                <!-- End ofBox Head -->
                                <div class="box-content">
                                    <div class="buttons">
                                        <input type="button" class="button" value="Xóa Thông Tin" name="btnDelete" 
                                               onclick="if(validateDeletingFunction('tblRecruitment', 'checkbox[]', 'msgOk')) submitForm();"/>
                                               <?php
                                               $tooltipText = "";
                                               $result = getAllRecruitment();
                                               $row = mysql_num_rows($result);
                                               if ($row >= 1) {
                                                   $tooltipText = "Không thể tạo thêm thông tin tuyển dụng, vui lòng chỉnh sửa thông tin cũ hoặc xoá và tạo mới!";
                                                   ?>
                                            <input type = "button" class = "button" disabled title = "<?php echo $tooltipText; ?>" value = "Thêm Thông Tin" onclick = "window.location = 'addRecruitment.php'"/>
                                            <?php
                                        } else {
                                            $status = "true";
                                            $tooltipText = "";
                                            ?>
                                            <input type = "button" class = "button" title = "<?php echo $tooltipText; ?>" value = "Thêm Thông Tin" onclick = "window.location = 'addRecruitment.php'"/>
                                            <?php
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- End Content -->
                        <div class="cl">&nbsp;</div>			
                    </div>
                    <!-- Main -->
                </div>
            </div>
            <!-- End Container -->
        </form>
        <!-- End Container -->

        <!--footer-->
        <?php include "footer.php"; ?>
        <!--end footer-->
        <?php ConnectionManager::getInstance()->closeConnection(); ?>
    </body>
</html>